package com.wzq.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wzq.app.R;
import com.wzq.app.adapter.UserAdapter;
import com.wzq.app.api.Api;
import com.wzq.app.api.ApiResponseCode;
import com.wzq.app.base.BaseActivity;
import com.wzq.app.constant.App;
import com.wzq.app.manager.UserManager;
import com.wzq.app.model.BaseModel;
import com.wzq.app.model.UserListModel;
import com.wzq.app.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: wzq
 * @Package: com.wzq.app.ui
 * @ClassName: UserListActivity
 * @Description: java类作用描述
 * @Author: Tao.xiaolong
 * @CreateDate: 2021/2/24 14:30
 * @UpdateUser: 更新者：
 * @UpdateDate: 2021/2/24 14:30
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class UserListActivity extends BaseActivity {
    private static final String TAG = "UserListActivity";

    @BindView(R.id.head_left)
    TextView headLeft;
    @BindView(R.id.head_left_text)
    TextView headLeftText;
    @BindView(R.id.head_right_text)
    TextView headRightText;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.list)
    ListView list;

    String type = "-1";
    String name = "";
    UserListModel userListModel;

    UserAdapter userAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.userlist_activity;
    }

    @Override
    public void configView() {
        type = getIntent().getStringExtra("type");
        name = getIntent().getStringExtra("name");
        headLeftText.setText(name);
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void requestData() {

        if ("-1".equals(type)) {
            return;
        }
        Map<String, Integer> map = new HashMap<>();
        if (type.equals(App.TEACHER) && App.TEACHER.equals(UserManager.getInstance().getUser().getType())) {

        } else {
            map.put("unit", Integer.valueOf(UserManager.getInstance().getUser().getUnit()));
        }
        map.put("offset", 0);
        map.put("page", 1);
        map.put("limit", 10);
        map.put("type", Integer.valueOf(type));
        Call<BaseModel<UserListModel>> call = Api.getInstance().service.getUserList(map);
        call.enqueue(new Callback<BaseModel<UserListModel>>() {
            @Override
            public void onResponse(Call<BaseModel<UserListModel>> call, Response<BaseModel<UserListModel>> response) {
                Log.d(TAG, response.toString());
                if (response.isSuccessful()) {
                    BaseModel<UserListModel> baseModel = response.body();
                    if (baseModel.code == ApiResponseCode.RESPHONE_OK) {
                        userListModel = baseModel.obj;
                        initList();
                    } else {
                        ToastUtils.showToast(mContext, "查询数据失败");
                    }
                } else {
                    ToastUtils.showToast(mContext, "查询数据失败");
                }
            }

            @Override
            public void onFailure(Call<BaseModel<UserListModel>> call, Throwable t) {
                ToastUtils.showToast(mContext, "查询数据失败");
            }
        });
    }

    private void initList() {
        userAdapter = new UserAdapter(userListModel, mContext);
        list.setAdapter(userAdapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.showToast(mContext, userListModel.getRows().get(position).getName());
                Intent intent = new Intent(mContext, RatingActivity.class);
                intent.putExtra("type", userListModel.getRows().get(position).getType());
                intent.putExtra("uid", userListModel.getRows().get(position).getId());
                intent.putExtra("name", userListModel.getRows().get(position).getName());
                if (UserManager.getInstance().getUser().getId() == userListModel.getRows().get(position).getId()) {
                    ToastUtils.showToast(mContext, "自己不能评价自己");
                    return;
                }
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
